$(document).ready(
    function() {

        makemarkitup("bbcode");

        new AjaxUpload(
            $("#picture"),
            {
                action: "/admin/imageUpload.php",
                name: "image",
                onSubmit : function(file, ext) {
                    if (! (ext && /^(jpg|png|jpeg|gif)$/i.test(ext))){
                        alert('Error: invalid file extension');
                        return false;
                    }

                    $("#bbcode").focus();
                    $("#bbcode").attr("disabled", "disabled");
                    $("img.imageLoader").attr("display", "block");
                    this.disable();
                },
                onComplete: function(file, response) {
                    $("img.imageLoader").attr("display", "none");
                    this.enable();
                    if(response != "false") {
                        if(confirm("Использовать это изображение для preview?")) {
                            getTextAreaSelection("bbcode").setSelectedTextWithClear("[img=preview]" + response + "[/img]");
                        } else {
                            getTextAreaSelection("bbcode").setSelectedTextWithClear("[img]" + response + "[/img]");
                        }
                    } else {
                        alert("error uploading the file.");
                    }
                    $("#bbcode").removeAttr("disabled");
                }
            }
        );
    }
);